import { createRouter, createWebHistory } from 'vue-router'
import gdata from './hooks/useGlobalData'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('./views/Home.vue'),
    meta: { title: gdata.CONST_DATA.APP_TITLE},
  },
  {
    path: '/main',
    name: 'Main',
    component: () => import('./views/Main.vue'),
    meta: { title: gdata.CONST_DATA.APP_TITLE},
  },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
  })
  
  // 无效路由跳转到首页
  router.beforeEach((to, from, next)=>{
    if (to.matched.length !== 0) {
      if (to.meta.title) {
        document.title = to.meta.title
      }
      next()
    } else {
      next({path: '/'})
    }
  })
  
  export default router